require 'open-uri'
require 'lecture'

PATTERN = ['<tr[^>]*>\s*',
'<td[^>]*>[^<]*<\/td>\s*',
'<td[^>]*>[^<]*<\/td>\s*',
'<td[^>]*>([^<]*)<\/td>\s*',
'<td[^>]*>[^<]*\s*',
'<a\s*href="([^"]*)"[^>]*>','\s*([^<]*)<\/a>\s*', # link, CLTR 
'</td>\s*',
'<td[^>]*>([^<]*)</td>\s*', # 인문학 글쓰기
'<td[^>]*>([^<]*)</td>\s*', # 3
'<td[^>]*>([^<]*)</td>\s*', # 3
'<td[^>]*>\s*',
'<br[^>]*>\s*',
'[^<]*</td>\s*',
'<td[^<]*>\s*',
'<nobr>([^<]*)</nobr>\s*', # 이원동
'<br[^>]*>\s*',
'</td>\s*',
'<td[^<]*>\s*',
'([^<]*)', # 화8b 9a 9b 목 9b 9a 9b
'<br[^>]*>\s*',
'</td>\s*',
'<td[^>]*>\s*', # 2합-301
'<nobr>([^<]*)</nobr>\s*'
].join()
# 1 => 자율전공부
# 2 = sylla_f.con?t_code=CLTR202...
# 3 = CLTR20201
def readUri(uri)
  result = ''
  open(uri) do |f|
    f.each do |line|
      result = result + line
    end
  end
  return result
end

def parser(str)
  result = []
  regex = Regexp.new(PATTERN, Regexp::IGNORECASE)
  matchdata = regex.match(str)
  count = 0
  while matchdata
    count = count + 1
#    for i in 1...matchdata.size() # exclude right bound
#      print i.to_s + ":"+matchdata[i].chomp()#+"\n"
#    end
    result.push(Lecture.new(matchdata[1],matchdata[2],matchdata[3],matchdata[4],matchdata[5],matchdata[6],matchdata[7].gsub(/\s/,''),matchdata[8],matchdata[9]))
    str = matchdata.post_match()
    matchdata = regex.match(str)
#    print "\n"
  end
#  print "count : " << count.to_s()
  return result
end
# 본 링크에 대해서는 갯수 83개 정확함
a = readUri('http://knusys6.knu.ac.kr/knu/owa3/sylla_f.lst?t_gubun=3&t_query1=1')
#print(a)
print parser(a)